package portBroadcasting;

import java.net.*;
import java.lang.*;

/**
 * User: Mike
 * Date: 12/12/11
 */

/**
 * This is the main class to broadcast temperature data from. compile this to MultiCastServer_v1.0.jar
 *
 * Multicast on my router
 * IGMP Multicast memberships
 * Multicast Group Address
 * 224.0.0.253
 * 224.0.0.251
 * 239.255.255.250
 * 224.0.0.252
 * 239.192.152.143
 * 224.5.6.74
 */

// todo write in a cmd line call to record all data to file & stop it & delete the file after it gets larger than some value..
// todo write in a cmd line call to clear the data file & start recording it again.

public class Server {

    static final int TCP_PORT = 1025;
    static final int MULTI_PORT = 1200;                     // port number should be more than 1024
    static final String I_NET_ADD = "224.224.224.224";        // address between 224.0.0.1 to 239.255.255.255
    private static final int SECONDS_TO_SLEEP = 1000;       // every 1 sec = 1000 ms


    public static void main(String args[]) throws Exception {
//        TCPconn();
        Multicast();
    }

    private static void Multicast() throws Exception {
        Temperature temps = new Temperature();
        MulticastSocket socket;
        DatagramPacket packet;
        InetAddress address = InetAddress.getByName(I_NET_ADD);
        socket = new MulticastSocket();
        socket.joinGroup(address);                          // join a Multicast group and send the group salutations
        byte[] data;
        boolean sendMessages = true;
        while (sendMessages) {
            Thread.sleep(SECONDS_TO_SLEEP);                             // every 1 sec = 1000 ms
            String str = temps.GetTempMessage();
            data = str.getBytes();
            packet = new DatagramPacket(data, str.length(), address, MULTI_PORT);
            socket.send(packet);                            // Sends the packet
        }
    }

//    private static void TCPconn() {
//        ServerSocket sersock;
//        Socket sock = null;
//        System.out.println(" Wait For It!");
//        try {
//            sersock = new ServerSocket(TCP_PORT);                               //  Initialising the ServerSocket
//            System.out.println("Server Started  :" + sersock);                  // Gives the Server Details Machine name, Port number
//            sock = sersock.accept();                                            // makes a socket connection to particular client after which two way communication take place
//            System.out.println("TempClient Connected  :" + sock);
//            DataInputStream ins = new DataInputStream(sock.getInputStream());   // Receive message from client i.e Request from client
//
//            PrintStream ios = new PrintStream(sock.getOutputStream());
//            ios.println("Hello from server");
//            try {
//                Thread.sleep((long) SECONDS_TO_SLEEP * 1000);                   // sleep for X ms
//            } catch (InterruptedException e) {
//                System.out.println("problem with sleeping: " + e.getMessage());
//            }
//            ios.close();
//            sock.close();
//        } catch (SocketException se) {
//            System.out.println("Server Socket problem  " + se.getMessage());
//        } catch (Exception e) {
//            System.out.println("Couldn't start " + e.getMessage());
//        }
//        System.out.println(" Connection from :  " + sock.getInetAddress());                 // Usage of some methods in Socket class
//    }
}